import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: "/login",
      name: "login",
      component: () => import('../views/login/index.vue')
    },
    {
      path: "/main",
      name: "main",
      component: () => import('../views/main/index.vue'),
      children: [
        {
          path: "home",
          name: "home",
          component: () => import('../views/main/home/index.vue')
        },{
          path: "menu",
          name: "menu",
          component: () => import('../views/main/menu/index.vue')
        },{
          path: "user",
          name: "user",
          component: () => import('../views/main/user/index.vue')
        },{
          path: "roles",
          name: "roles",
          component: () => import('../views/main/roles/index.vue')
        },{
          path: "material",
          name: "material",
          component: () => import('../views/main/material/index.vue')
        },{
          path: "document",
          name: "document",
          component: () => import('../views/main/document/index.vue')
        },{
          path: "outbound",
          name: "outbound",
          component: () => import('../views/main/outbound/index.vue')
        },{
          path: "warehousing",
          name: "warehousing",
          component: () => import('../views/main/warehousing/index.vue')
        },{
          path: "aggregation",
          name: "aggregation",
          component: () => import('../views/main/aggregation/index.vue')
        },
      ]
    },
    // {
    //   path: "/system",
    //   name: "system",
    //   component: () => import('../views/system/index.vue')
    // },
    {
      path: "/",
      redirect: "/login"
    },
    {
      path: "/:pathMatch(.*)*",
      component: () => import('../views/system/notFound.vue')
    }
  ]
});

const whiteList = ["/", "/login"]; 

router.beforeEach((to, from, next) => {
  const token = localStorage.getItem("token");
  if (whiteList.includes(to.path) || token) {
    next();
  } else {
    next({ path: "/login" });
  }
});


export default router;
